﻿using AwesomiumDotNet;
using System;
using System.Drawing;
using System.Windows.Forms;

namespace FormsSample
{
    public partial class WebForm : Form
    {
        WebCore webCore;
        WebView webView;
        Timer timer;

        public WebForm()
        {
            InitializeComponent();
            BackColor = Color.White;

            Resize += new EventHandler(WebForm_Resize);
            webViewBitmap.MouseMove += new MouseEventHandler(WebForm_MouseMove);
            webViewBitmap.MouseDown += new MouseEventHandler(WebForm_MouseDown);
            webViewBitmap.MouseUp += new MouseEventHandler(WebForm_MouseUp);
            MouseWheel += new MouseEventHandler(WebForm_MouseWheel);
            KeyDown += new KeyEventHandler(WebForm_KeyDown);
            KeyPress += new KeyPressEventHandler(WebForm_KeyPress);
            FormClosed += new FormClosedEventHandler(WebForm_FormClosed);

            WebCoreOptions options = new WebCoreOptions();
            options.EnablePlugins = true;
            options.LogLevel = LogLevel.None;
            webCore = new WebCore(options);

            webView = webCore.CreateWebView(webViewBitmap.Width, webViewBitmap.Height, false, true);
            webView.ChangeCursor += new EventHandler<ChangeCursorEventArgs>(webView_ChangeCursor);
            webView.LoadUrl("http://www.google.com");
            GetResizedBitmap();

            timer = new Timer();
            timer.Interval = 30;
            timer.Tick += new EventHandler(timer_Tick);
            timer.Start();
        }

        void WebForm_FormClosed(object sender, FormClosedEventArgs e)
        {
            webView.Dispose();
            webCore.Dispose();
        }

        void timer_Tick(object sender, EventArgs e)
        {
            webCore.Update();
            if (webView.IsDirty())
                Render();
        }

        void Render()
        {
            webView.RenderToBitmap((Bitmap)webViewBitmap.Image);
            webViewBitmap.Invalidate();
        }

        void GetResizedBitmap()
        {
            // Dimensions may be zero when the window is minimized or collapsed.
            if (webViewBitmap.Height == 0)
                return;

            webViewBitmap.Image = new Bitmap(webViewBitmap.Width, webViewBitmap.Height, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
        }

        void WebForm_Resize(object sender, EventArgs e)
        {
            GetResizedBitmap();
            webView.Resize(webViewBitmap.Width, webViewBitmap.Height);
            Render();
        }

        void webView_ChangeCursor(object sender, ChangeCursorEventArgs e)
        {
            Cursor = e.Cursor;
        }

        void WebForm_MouseWheel(object sender, MouseEventArgs e)
        {
            webView.InjectMouseWheel(e.Delta);
        }

        void WebForm_KeyPress(object sender, KeyPressEventArgs e)
        {
            WebKeyboardEvent keyboardEvent = new WebKeyboardEvent(WM.Char, (uint)e.KeyChar, 0);
            webView.InjectKeyboardEvent(keyboardEvent);
        }

        void WebForm_KeyDown(object sender, KeyEventArgs e)
        {
            WebKeyboardEvent keyboardEvent = new WebKeyboardEvent(WM.KeyDown, (uint)e.KeyValue, 0);
            webView.InjectKeyboardEvent(keyboardEvent);
        }

        void WebForm_MouseUp(object sender, MouseEventArgs e)
        {
            webView.InjectMouseUp(e.Button);
        }

        void WebForm_MouseDown(object sender, MouseEventArgs e)
        {
            webView.InjectMouseDown(e.Button);
        }

        void WebForm_MouseMove(object sender, MouseEventArgs e)
        {
            webView.InjectMouseMove(e.Location);
        }
    }
}
